﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Com.CMS263.Util
{
    public static class Pager
    {
        /// <summary>
        /// 上一页下一页通用方法
        /// </summary>
        /// <param name="count">信息总条数</param>
        /// <param name="pagesize">每页显示条数</param>
        /// <param name="CurrentIndex">当前第几页</param>
        /// <returns></returns>
        public static string GetPager(int count, int pagesize, int CurrentIndex, string type)
        {

            StringBuilder sb = new StringBuilder();
            StringBuilder sb1 = new StringBuilder();
            int pageCount = ((count % pagesize) == 0 ? count / pagesize : count / pagesize + 1);

            sb1.Append("<input type='text' name='pagetext' id='pagetext' style='height:12px; width:20px' value='" + CurrentIndex + "' onblur='checkPageNum(" + pageCount + ")' ><a href='javascript:pageUrl(" + type + ")'>GO</a>");

            if (count > 0)
            {


                sb.Append("共" + count + "条 当前" + CurrentIndex + "/" + pageCount + "页 ");
                if (CurrentIndex == 1)
                {
                    sb.Append("<a>上一页</a>");
                }
                else if (pageCount == 1)
                {
                    sb.Append("<a>上一页</a>");
                }
                else
                {
                    sb.Append("<a href='?page=" + (CurrentIndex - 1) + "&type=" + type + "'>上一页</a>");
                }
                if (pageCount <= 5)
                {
                    for (int i = 1; i <= pageCount; i++)
                    {
                        if (i == CurrentIndex)
                        {
                            sb.Append("<a class='now'>" + i + "</b>");
                        }
                        else
                        {
                            sb.Append("<a href='?page=" + i + "&type=" + type + "'>" + i + "</a>");
                        }
                    }
                    if (CurrentIndex == pageCount)
                    {
                        sb.Append("<a>下一页</a>");
                    }
                    else if (pageCount == 1)
                    {
                        sb.Append("<a>下一页</a>");
                    }
                    else
                    {
                        sb.Append("<a href='?page=" + (CurrentIndex + 1) + "&type=" + type + "'>下一页</a>");
                    }
                }
                else
                {

                    if (CurrentIndex == 1)
                    {
                        for (int i = 1; i <= 5; i++)
                        {
                            if (i == CurrentIndex)
                            {
                                sb.Append("<a class='now'>" + i + "</b>");
                            }
                            else
                            {
                                sb.Append("<a href='?page=" + i + "&type=" + type + "'>[" + i + "]</a>");
                            }
                        }
                        if (CurrentIndex == pageCount)
                        {
                            sb.Append("<a>下一页</a>");
                        }
                        else if (pageCount == 1)
                        {
                            sb.Append("<a>下一页</a>");
                        }
                        else
                        {
                            sb.Append("<a href='?page=" + (CurrentIndex + 1) + "&type=" + type + "'>下一页</a>");
                        }
                    }
                    else if (CurrentIndex == 2)
                    {
                        for (int i = 1; i <= 5; i++)
                        {
                            if (i == CurrentIndex)
                            {
                                sb.Append("<a class='now'>" + i + "</b>");
                            }
                            else
                            {
                                sb.Append("<a href='?page=" + i + "&type=" + type + "'>[" + i + "]</a>");
                            }
                        }
                        if (CurrentIndex == pageCount)
                        {
                            sb.Append("<a>下一页</a>");
                        }
                        else if (pageCount == 1)
                        {
                            sb.Append("<a>下一页</a>");
                        }
                        else
                        {
                            sb.Append("<a href='?page=" + (CurrentIndex + 1) + "&type=" + type + "'>下一页</a>");
                        }
                    }
                    else if (CurrentIndex == 3)
                    {
                        for (int i = 1; i <= CurrentIndex + 2; i++)
                        {
                            if (i == CurrentIndex)
                            {
                                sb.Append("<a class='now'>" + i + "</b>");
                            }
                            else
                            {
                                sb.Append("<a href='?page=" + i + "&type=" + type + "'>[" + i + "]</a>");
                            }
                        }
                        if (CurrentIndex == pageCount)
                        {
                            sb.Append("<a><a>下一页</a></a>");
                        }
                        else if (pageCount == 1)
                        {
                            sb.Append("<a><a>下一页</a></a>");
                        }
                        else
                        {
                            sb.Append("<a href='?page=" + (CurrentIndex + 1) + "&type=" + type + "'>下一页</a>");
                        }
                    }
                    else if (pageCount - CurrentIndex < 3)
                    {
                        for (int i = pageCount - 5; i <= pageCount; i++)
                        {
                            if (i == CurrentIndex)
                            {
                                sb.Append("<b>" + i + "</b>");
                            }
                            else
                            {
                                sb.Append("<a href='?page=" + i + "&type=" + type + "'>[" + i + "]</a>");
                            }
                        }
                        if (CurrentIndex == pageCount)
                        {
                            sb.Append("<a>下一页</a>");
                        }
                        else if (pageCount == 1)
                        {
                            sb.Append("<a>下一页</a>");
                        }
                        else
                        {
                            sb.Append("<a href='?page=" + (CurrentIndex + 1) + "&type=" + type + "'>下一页</a>");
                        }

                    }
                    else
                    {
                        for (int i = CurrentIndex - 2; i <= CurrentIndex + 2; i++)
                        {
                            if (i == CurrentIndex)
                            {
                                sb.Append("<b>" + i + "</b>");
                            }
                            else
                            {
                                sb.Append("<a href='?page=" + i + "&type=" + type + "'>[" + i + "]</a>");
                            }
                        }
                        if (CurrentIndex == pageCount)
                        {
                            sb.Append("<a>下一页</a>");
                        }
                        else if (pageCount == 1)
                        {
                            sb.Append("<a>下一页</a>");
                        }
                        else
                        {
                            sb.Append("<a href='?page=" + (CurrentIndex + 1) + "&type=" + type + "'>下一页</a>");
                        }
                    }


                } return sb.ToString() + sb1.ToString();

            }
            else
            {
                sb.Append("暂时没有相关信息");
                return sb.ToString();
            }

        }

        public static string GetEnPager(int count, int pagesize, int CurrentIndex, string type)
        {

            StringBuilder sb = new StringBuilder();
            StringBuilder sb1 = new StringBuilder();
            int pageCount = ((count % pagesize) == 0 ? count / pagesize : count / pagesize + 1);

            sb1.Append("<input type='text' name='pagetext' id='pagetext' style='height:12px; width:20px' value='" + CurrentIndex + "' onblur='checkPageNum(" + pageCount + ")' ><a href='javascript:pageUrl(" + type + ")'>GO</a>");

            if (count > 0)
            {


                sb.Append("Total:" + count + " Current" + CurrentIndex + "/" + pageCount + "P ");
                if (CurrentIndex == 1)
                {
                    sb.Append("<a>Prev</a>");
                }
                else if (pageCount == 1)
                {
                    sb.Append("<a>Prev</a>");
                }
                else
                {
                    sb.Append("<a href='?page=" + (CurrentIndex - 1) + "&type=" + type + "'>Prev</a>");
                }
                if (pageCount <= 5)
                {
                    for (int i = 1; i <= pageCount; i++)
                    {
                        if (i == CurrentIndex)
                        {
                            sb.Append("<a class='now'>" + i + "</b>");
                        }
                        else
                        {
                            sb.Append("<a href='?page=" + i + "&type=" + type + "'>" + i + "</a>");
                        }
                    }
                    if (CurrentIndex == pageCount)
                    {
                        sb.Append("<a>Next</a>");
                    }
                    else if (pageCount == 1)
                    {
                        sb.Append("<a>Next</a>");
                    }
                    else
                    {
                        sb.Append("<a href='?page=" + (CurrentIndex + 1) + "&type=" + type + "'>Next</a>");
                    }
                }
                else
                {

                    if (CurrentIndex == 1)
                    {
                        for (int i = 1; i <= 5; i++)
                        {
                            if (i == CurrentIndex)
                            {
                                sb.Append("<a class='now'>" + i + "</b>");
                            }
                            else
                            {
                                sb.Append("<a href='?page=" + i + "&type=" + type + "'>[" + i + "]</a>");
                            }
                        }
                        if (CurrentIndex == pageCount)
                        {
                            sb.Append("<a>Next</a>");
                        }
                        else if (pageCount == 1)
                        {
                            sb.Append("<a>Next</a>");
                        }
                        else
                        {
                            sb.Append("<a href='?page=" + (CurrentIndex + 1) + "&type=" + type + "'>Next</a>");
                        }
                    }
                    else if (CurrentIndex == 2)
                    {
                        for (int i = 1; i <= 5; i++)
                        {
                            if (i == CurrentIndex)
                            {
                                sb.Append("<a class='now'>" + i + "</b>");
                            }
                            else
                            {
                                sb.Append("<a href='?page=" + i + "&type=" + type + "'>[" + i + "]</a>");
                            }
                        }
                        if (CurrentIndex == pageCount)
                        {
                            sb.Append("<a>Next</a>");
                        }
                        else if (pageCount == 1)
                        {
                            sb.Append("<a>Next</a>");
                        }
                        else
                        {
                            sb.Append("<a href='?page=" + (CurrentIndex + 1) + "&type=" + type + "'>Next</a>");
                        }
                    }
                    else if (CurrentIndex == 3)
                    {
                        for (int i = 1; i <= CurrentIndex + 2; i++)
                        {
                            if (i == CurrentIndex)
                            {
                                sb.Append("<a class='now'>" + i + "</b>");
                            }
                            else
                            {
                                sb.Append("<a href='?page=" + i + "&type=" + type + "'>[" + i + "]</a>");
                            }
                        }
                        if (CurrentIndex == pageCount)
                        {
                            sb.Append("<a><a>Next</a></a>");
                        }
                        else if (pageCount == 1)
                        {
                            sb.Append("<a><a>Next</a></a>");
                        }
                        else
                        {
                            sb.Append("<a href='?page=" + (CurrentIndex + 1) + "&type=" + type + "'>Next</a>");
                        }
                    }
                    else if (pageCount - CurrentIndex < 3)
                    {
                        for (int i = pageCount - 5; i <= pageCount; i++)
                        {
                            if (i == CurrentIndex)
                            {
                                sb.Append("<b>" + i + "</b>");
                            }
                            else
                            {
                                sb.Append("<a href='?page=" + i + "&type=" + type + "'>[" + i + "]</a>");
                            }
                        }
                        if (CurrentIndex == pageCount)
                        {
                            sb.Append("<a>Next</a>");
                        }
                        else if (pageCount == 1)
                        {
                            sb.Append("<a>Next</a>");
                        }
                        else
                        {
                            sb.Append("<a href='?page=" + (CurrentIndex + 1) + "&type=" + type + "'>Next</a>");
                        }

                    }
                    else
                    {
                        for (int i = CurrentIndex - 2; i <= CurrentIndex + 2; i++)
                        {
                            if (i == CurrentIndex)
                            {
                                sb.Append("<b>" + i + "</b>");
                            }
                            else
                            {
                                sb.Append("<a href='?page=" + i + "&type=" + type + "'>[" + i + "]</a>");
                            }
                        }
                        if (CurrentIndex == pageCount)
                        {
                            sb.Append("<a>Next</a>");
                        }
                        else if (pageCount == 1)
                        {
                            sb.Append("<a>Next</a>");
                        }
                        else
                        {
                            sb.Append("<a href='?page=" + (CurrentIndex + 1) + "&type=" + type + "'>Next</a>");
                        }
                    }


                } return sb.ToString() + sb1.ToString();

            }
            else
            {
                sb.Append("No Result");
                return sb.ToString();
            }

        }
        public static string GetPageNew(int count, int pagesize, int CurrentIndex, string type, string page)
        {

            StringBuilder sb = new StringBuilder();

            int pageCount = ((count % pagesize) == 0 ? count / pagesize : count / pagesize + 1);
            if (count > 0)
            {
                sb.Append("<span>第&nbsp;" + CurrentIndex + "/" + pageCount + "&nbsp;页;每页&nbsp;" + pagesize + "&nbsp;条;共&nbsp;" + count + "&nbsp;条记录&nbsp;&nbsp;&nbsp;&nbsp;");
                if (pageCount == 1)
                {
                    sb.Append("<a style='text-decoration: none' ><img style='border:0px' src='/manage/images/fy_01.gif' alt='首页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><img style='border:0px' src='/manage/images/fy_02.gif' alt='上一页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><input type='text' class='input_text' size='4' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><img style='border:0px' src='/manage/images/fy_03.gif' alt='跳页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><img style='border:0px' src='/manage/images/fy_04.gif' alt='下一页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><img style='border:0px' src='/manage/images/fy_05.gif' alt='末页' />&nbsp;&nbsp;&nbsp;</a></span>");
                }
                else if (CurrentIndex == 1)
                {
                    sb.Append("<a style='text-decoration: none' ><img style='border:0px' src='/manage/images/fy_01.gif' alt='首页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><img style='border:0px' src='/manage/images/fy_02.gif' alt='上一页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><input id='pagetext' type='text' class='input_text' size='4' value='" + CurrentIndex + "' onblur='checkPageNum(" + pageCount + ")' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' href='javascript:pagerUrl(" + page + ")'><input type='hidden' value='" + type + "' id='pager_type' name='pager_type'><img style='border:0px' src='/manage/images/fy_03.gif' alt='跳页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' href='?" + page + "=" + (CurrentIndex + 1) + type + "'><img style='border:0px' src='/manage/images/fy_04.gif' alt='下一页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' href='?" + page + "=" + pageCount + type + "'><img style='border:0px' src='/manage/images/fy_05.gif' alt='末页' />&nbsp;&nbsp;&nbsp;</a></span>");
                }
                else if (CurrentIndex == pageCount)
                {
                    sb.Append("<a style='text-decoration: none' href='?" + page + "=1" + type + "'><img style='border:0px' src='/manage/images/fy_01.gif' alt='首页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' href='?" + page + "=" + (CurrentIndex - 1) + type + "'><img style='border:0px' src='/manage/images/fy_02.gif' alt='上一页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><input id='pagetext' type='text' class='input_text' size='4' value='" + CurrentIndex + "' onblur='checkPageNum(" + pageCount + ")' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' href='javascript:pagerUrl(" + page + ")'><input type='hidden' value='" + type + "' id='pager_type' name='pager_type'><img style='border:0px' src='/manage/images/fy_03.gif' alt='跳页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><img style='border:0px' src='/manage/images/fy_04.gif' alt='下一页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><img style='border:0px' src='/manage/images/fy_05.gif' alt='末页' />&nbsp;&nbsp;&nbsp;</a></span>");
                }
                else
                {
                    sb.Append("<a style='text-decoration: none' href='?" + page + "=1" + type + "'><img style='border:0px' src='/manage/images/fy_01.gif' alt='首页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' href='?" + page + "=" + (CurrentIndex - 1) + type + "'><img style='border:0px' src='/manage/images/fy_02.gif' alt='上一页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' ><input id='pagetext' type='text' class='input_text' size='4' value='" + CurrentIndex + "' onblur='checkPageNum(" + pageCount + ")' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' href='javascript:pagerUrl(" + page + ")'><input type='hidden' value='" + type + "' id='pager_type' name='pager_type'><img style='border:0px' src='/manage/images/fy_03.gif' alt='跳页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' href='?" + page + "=" + (CurrentIndex + 1) + type + "'><img style='border:0px' src='/manage/images/fy_04.gif' alt='下一页' />&nbsp;&nbsp;&nbsp;</a>");
                    sb.Append("<a style='text-decoration: none' href='?" + page + "=" + pageCount + type + "'><img style='border:0px' src='/manage/images/fy_05.gif' alt='末页' />&nbsp;&nbsp;&nbsp;</a></span>");
                }
            }
            else
            {
                sb.Append("<span>抱歉,没有找到相关数据</span>");
            }
            return sb.ToString();
        }

        public static string GetSinglePage(int count, int pagesize, int CurrentIndex, string type, string page)
        {

            StringBuilder sb = new StringBuilder();

            int pageCount = ((count % pagesize) == 0 ? count / pagesize : count / pagesize + 1);
            if (count > 0)
            {
                if (pageCount == 1)
                {


                    sb.Append("<a href='#'><img src='images/p_pre.gif' width='66' height='28' /></a>");
                    sb.Append("<a href='#'><img src='images/p_next.gif' width='66' height='28' /></a>");
                }
                else if (CurrentIndex == 1)
                {
                    sb.Append("<a href='#'><img src='images/p_pre.gif' width='66' height='28' /></a>");
                    sb.Append("<a href='?" + page + "=" + (CurrentIndex + 1) + type + "'><img src='images/p_next.gif' width='66' height='28' /></a>");

                }
                else if (CurrentIndex == pageCount)
                {
                    sb.Append("<a href='?" + page + "=" + (CurrentIndex - 1) + type + "' ><img src='images/p_pre.gif' width='66' height='28' /></a>");
                    sb.Append("<a href='#' ><img src='images/p_next.gif' width='66' height='28' /></a>");
                }
                else
                {
                    sb.Append("<a href='?" + page + "=" + (CurrentIndex - 1) + type + "' ><img src='images/p_pre.gif' width='66' height='28' /></a>");

                    sb.Append("<a href='?" + page + "=" + (CurrentIndex + 1) + type + "'><img src='images/p_next.gif' width='66' height='28' /></a>");

                }
            }
            else
            {
                sb.Append("<span>抱歉,没有找到相关数据</span>");
            }
            return sb.ToString();
        }

        public static string GetShowPager(int count, int pagesize, int CurrentIndex, string type)
        {

            StringBuilder sb = new StringBuilder();
            StringBuilder sb1 = new StringBuilder();
            int pageCount = ((count % pagesize) == 0 ? count / pagesize : count / pagesize + 1);

            sb1.Append("<input type='text' name='pagetext' id='pagetext' style='height:12px; width:20px' value='" + CurrentIndex + "' onblur='checkPageNum(" + pageCount + ")' ><a href='javascript:pageUrl(" + type + ")'>GO</a>");

            if (count > 0)
            {


                sb.Append("共" + count + "条 当前" + CurrentIndex + "/" + pageCount + "页 ");
                if (CurrentIndex == 1)
                {
                    sb.Append("上一页");
                }
                else if (pageCount == 1)
                {
                    sb.Append("上一页");
                }
                else
                {
                    sb.Append("<a href='?page=" + (CurrentIndex - 1) + "&type=" + type + "'>上一页</a>");
                }
                if (pageCount <= 5)
                {
                    for (int i = 1; i <= pageCount; i++)
                    {
                        if (i == CurrentIndex)
                        {
                            sb.Append("<b>" + i + "</b>");
                        }
                        else
                        {
                            sb.Append("<a href='?page=" + i + "&type=" + type + "'>" + i + "</a>");
                        }
                    }
                    if (CurrentIndex == pageCount)
                    {
                        sb.Append("下一页");
                    }
                    else if (pageCount == 1)
                    {
                        sb.Append("下一页");
                    }
                    else
                    {
                        sb.Append("<a href='?page=" + (CurrentIndex + 1) + "&type=" + type + "'>下一页</a>");
                    }
                }
                else
                {

                    if (CurrentIndex == 1)
                    {
                        for (int i = 1; i <= 5; i++)
                        {
                            if (i == CurrentIndex)
                            {
                                sb.Append("<b>" + i + "</b>");
                            }
                            else
                            {
                                sb.Append("<a href='?page=" + i + "&type=" + type + "'>[" + i + "]</a>");
                            }
                        }
                        if (CurrentIndex == pageCount)
                        {
                            sb.Append("下一页");
                        }
                        else if (pageCount == 1)
                        {
                            sb.Append("下一页");
                        }
                        else
                        {
                            sb.Append("<a href='?page=" + (CurrentIndex + 1) + "&type=" + type + "'>下一页</a>");
                        }
                    }
                    else if (CurrentIndex == 2)
                    {
                        for (int i = 1; i <= 5; i++)
                        {
                            if (i == CurrentIndex)
                            {
                                sb.Append("<b>" + i + "</b>");
                            }
                            else
                            {
                                sb.Append("<a href='?page=" + i + "&type=" + type + "'>[" + i + "]</a>");
                            }
                        }
                        if (CurrentIndex == pageCount)
                        {
                            sb.Append("下一页");
                        }
                        else if (pageCount == 1)
                        {
                            sb.Append("下一页");
                        }
                        else
                        {
                            sb.Append("<a href='?page=" + (CurrentIndex + 1) + "&type=" + type + "'>下一页</a>");
                        }
                    }
                    else if (CurrentIndex == 3)
                    {
                        for (int i = 1; i <= CurrentIndex + 2; i++)
                        {
                            if (i == CurrentIndex)
                            {
                                sb.Append("<b>" + i + "</b>");
                            }
                            else
                            {
                                sb.Append("<a href='?page=" + i + "&type=" + type + "'>[" + i + "]</a>");
                            }
                        }
                        if (CurrentIndex == pageCount)
                        {
                            sb.Append("下一页");
                        }
                        else if (pageCount == 1)
                        {
                            sb.Append("下一页");
                        }
                        else
                        {
                            sb.Append("<a href='?page=" + (CurrentIndex + 1) + "&type=" + type + "'>下一页</a>");
                        }
                    }
                    else if (pageCount - CurrentIndex < 3)
                    {
                        for (int i = pageCount - 5; i <= pageCount; i++)
                        {
                            if (i == CurrentIndex)
                            {
                                sb.Append("<b>" + i + "</b>");
                            }
                            else
                            {
                                sb.Append("<a href='?page=" + i + "&type=" + type + "'>[" + i + "]</a>");
                            }
                        }
                        if (CurrentIndex == pageCount)
                        {
                            sb.Append("下一页");
                        }
                        else if (pageCount == 1)
                        {
                            sb.Append("下一页");
                        }
                        else
                        {
                            sb.Append("<a href='?page=" + (CurrentIndex + 1) + "&type=" + type + "'>下一页</a>");
                        }

                    }
                    else
                    {
                        for (int i = CurrentIndex - 2; i <= CurrentIndex + 2; i++)
                        {
                            if (i == CurrentIndex)
                            {
                                sb.Append("<b>" + i + "</b>");
                            }
                            else
                            {
                                sb.Append("<a href='?page=" + i + "&type=" + type + "'>[" + i + "]</a>");
                            }
                        }
                        if (CurrentIndex == pageCount)
                        {
                            sb.Append("下一页");
                        }
                        else if (pageCount == 1)
                        {
                            sb.Append("下一页");
                        }
                        else
                        {
                            sb.Append("<a href='?page=" + (CurrentIndex + 1) + "&type=" + type + "'>下一页</a>");
                        }
                    }


                } return sb.ToString() + sb1.ToString();

            }
            else
            {
                sb.Append("暂时没有相关信息");
                return sb.ToString();
            }
        }

    }
}
